package com.bai.qf2201.controller;

import com.bai.qf2201.po.Orders;
import com.bai.qf2201.service.Impl.cityServiceImpl;
import com.bai.qf2201.service.carService;
import com.bai.qf2201.service.cityService;
import com.bai.qf2201.service.orderService;
import com.bai.qf2201.vo.PageOrder;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private orderService oS;
    @Autowired
    private carService cS;
    @Resource
    private cityService cS2;


    @ResponseBody
    @RequestMapping("/addOrder")
    public int addOrder(Orders orders){
        System.out.println(orders);
        int i = oS.addOne(orders);
        return 1;
    }

    @ResponseBody
    @RequestMapping("/AllOrder")
    public List<PageOrder> findAll(int uid){

        List<Orders> all = oS.findByUid(uid);

        List<PageOrder> p=new ArrayList<>();
        for (Orders orders : all) {
            PageOrder pageOrder = new PageOrder();
            pageOrder.setOid(orders.getId());
            pageOrder.setGetcity(cS2.findById2(orders.getGetid()).getName());
            pageOrder.setBackcity(cS2.findById2(orders.getGetid()).getName());
            pageOrder.setCname(cS.findOneById(orders.getCid()).getName());
            pageOrder.setOprice(orders.getOprice());
            pageOrder.setOstatus(orders.getStatus());
            p.add(pageOrder);
        }
        return p;
    }

    @ResponseBody
    @RequestMapping("/delOrder")
    public int delOrder(int  id){
        int i = oS.delOne(id);
        return i;
    }
}
